var SL = require('./schoolList.js');

Page({
  data: {
    teacherInfo:{},
    schoolList:[],  //总列表
    schoolList_now:[], //当前使用的列表：适用于搜索
    choose_school:{
      name:'',
      is985:false,
      is211:false
    },
    imageList:[],//本地缓存的图片
    bh:'',//北航南航单独处理
    nh:'',
  },

  /**
   * 生命周期
   * 载入
   */
  onLoad(options){
    var teacherInfo = JSON.parse(options['jsonData'])
    this.setData({
      teacherInfo:teacherInfo
    })
    this.getSchoolList()

    /*  资源的问题，以后再解决
    this.downBHNH().then((resolve)=>{
      console.log('南航北航处理完毕，继续')
      this.downImageAndStorage()
    })
    */
  },

  /**
   * 自定义学校名称
   */
  choose_button(e){
    var teacherInfo = this.data.teacherInfo
    teacherInfo.school.name = this.data.choose_school.name
    teacherInfo.school.is211 = this.data.choose_school.is211
    teacherInfo.school.is985 = this.data.choose_school.is985

    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面

    prevPage.setData({
      teacherInfo: teacherInfo,
      isErr:false
    })

    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 单独下载南航北航
   */
  downBHNH(){
    var that =this
    var bhnh = [
      'cloud://cloud1-2g7tfhqmd89e6391.636c-cloud1-2g7tfhqmd89e6391-1307447694/bh.jpg',
      'cloud://cloud1-2g7tfhqmd89e6391.636c-cloud1-2g7tfhqmd89e6391-1307447694/nh.jpg'
    ]
    var countList = []
    return new Promise((resolve, rejcet)=>{
      for(var i = 0 ; i < bhnh.length; i++){
        wx.cloud.downloadFile({
          fileID:bhnh[i],
          success(res){
            console.log('downBHNH云储存',res)
            const fx = wx.getFileSystemManager()//获取全局文件管理器
            fx.saveFile({
              tempFilePath:res.tempFilePath,
              success(res){
                //console.log('保存图片到本地成功',res)
                countList.push(res.savedFilePath)
                if(countList.length == 2){
                  that.setData({
                    bh:countList[0],
                    nh:countList[1]
                  })
                  resolve()
                  console.log('北航南航处理完毕')
                }
              },
              fail(err){
                console.log(err)
              }
            })
    
          },
          fail(err){
            console.log('downBHNH云储存',err)
          }
        })
      }
    })
  },

  /**
   * 下载图片到本地的promise
   */
  downImageAndStorage_one(){
    return new Promise((resolve, rejcet)=>{

    })
  },

  /**
   * 下载图片到本地，并缓存到本地
   */
  downImageAndStorage(){
    var that = this
    var schoolList = this.data.schoolList
    var imageStorageList = [] // 保存在本地的图片list。应该放入缓存中
    var fileID 
    const fx = wx.getFileSystemManager()//获取全局文件管理器
    // 循环完成下载、加入缓存列表的步骤
    for(var i = 0; i < schoolList.length; i++){
      console.log('当前下载index：'+ i  +'，总index：'+schoolList.length)
      fileID = 'cloud://cloud1-2g7tfhqmd89e6391.636c-cloud1-2g7tfhqmd89e6391-1307447694/schoolImages/' + schoolList[i].name + '.jpg'
      if(schoolList[i].name == '北京航空航天大学'){
        console.log('跳过本次北航')
        imageStorageList.push(this.data.bh)
        continue
      }
      if(schoolList[i].name == '南京航空航天大学'){
        console.log('跳过本次南航')
        imageStorageList.push(this.data.nh)
        continue
      }
      console.log(schoolList[i].name)
      wx.cloud.downloadFile({
        fileID: fileID,
        success: res => {
          //console.log('云储存下载图片成功',res)
          fx.saveFile({
            tempFilePath:res.tempFilePath,
            success(res){
              //console.log('保存图片到本地成功',res)
              imageStorageList.push(res.savedFilePath)
              //如果数量够了，说明已经做完了循环
              console.log('检查数量:',imageStorageList.length,'检查数量2：',schoolList.length)
              if(imageStorageList.length == schoolList.length){
                console.log('循环下载保存结束')
                that.setData({
                  imageList:imageStorageList
                })
                wx.setStorageSync('imageStorageList', imageStorageList)
              }
            },
            fail(err){
              console.log('保存图片到本地失败',err)
            }
          })
        },
        fail: err => {
          console.log('云储存下载图片失败',err)
        }
      })
    }
  },

  /**
   * 加载图片失败
   */
  geterr(e){
    console.log('加载图片失败',e)
    var index = e.currentTarget.dataset.index
    var schoolList_now = this.data.schoolList_now
    schoolList_now[index]['isErr'] = true
    this.setData({
      schoolList_now:schoolList_now
    })
  },

  /**
   * 选择
   * 
   */  
  choose(e){
    console.log(e)
    var item = e.currentTarget.dataset.item
    
    console.log(e.currentTarget.dataset)
    var teacherInfo = this.data.teacherInfo
    console.log(this.data.teacherInfo)
    teacherInfo.school.name = item.name
    teacherInfo.school.is985 = item.is985
    teacherInfo.school.is211 = item.is211
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      teacherInfo: teacherInfo,
      isErr:false
    })
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 姓名输入
   */
  input_school(e){
    var value = e.detail.value
    var choose_school = this.data.choose_school
    choose_school.name = value
    this.setData({
      choose_school:choose_school
    })
  },

  /**
   * 获取schoolList
   */
  getSchoolList(){
    var schoolList = SL

    this.setData({
      schoolList:schoolList,
      schoolList_now:schoolList
    })
  },
  
  /**
   * 搜索学校
   */
  search_school(e){
    var value = e.detail.value
    var schoolList = this.data.schoolList
    var schoolList_now = []
    for(var i = 0; i < schoolList.length; i++){
      if(schoolList[i].name.indexOf(value) != -1){
        schoolList_now.push(schoolList[i])
      }
    }
    this.setData({
      schoolList_now:schoolList_now
    })
  }

})
